# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package(
    default_visibility = ["//visibility:public"],
    licenses = ["notice"],
)

p4rt_version = "0.0.1"

p4rt_maintainer = "PINS Working Group <pins-wg@opennetworking.org>"

load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")

cc_binary(
    name = "p4rt",
    srcs = ["p4rt.cc"],
    deps = [
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_absl//absl/functional:bind_front",
        "@com_google_absl//absl/log:initialize",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_grpc_grpc//:grpc++_authorization_provider",
        "@com_github_grpc_grpc//:grpc_security_base",
        "//gutil/gutil:status",
        "//gutil/gutil:syslog_sink",
        "//p4rt_app/event_monitoring:app_state_db_port_table_event",
        "//p4rt_app/event_monitoring:app_state_db_send_to_ingress_port_table_event",
        "//p4rt_app/event_monitoring:config_db_node_cfg_table_event",
        "//p4rt_app/event_monitoring:config_db_port_table_event",
	"//p4rt_app/event_monitoring:config_db_queue_table_event",
        "//p4rt_app/event_monitoring:debug_data_dump_events",
        "//p4rt_app/event_monitoring:state_event_monitor",
        "//p4rt_app/event_monitoring:state_verification_events",
        "//p4rt_app/p4runtime:p4runtime_impl",
        "//p4rt_app/sonic:packetio_impl",
        "//p4rt_app/sonic:redis_connections",
        "//p4rt_app/sonic/adapters:consumer_notifier_adapter",
        "//p4rt_app/sonic/adapters:notification_producer_adapter",
        "//p4rt_app/sonic/adapters:producer_state_table_adapter",
        "//p4rt_app/sonic/adapters:system_call_adapter",
        "//p4rt_app/sonic/adapters:table_adapter",
        "//p4rt_app/sonic/adapters:warm_boot_state_adapter",
        "@sonic_swss_common//:libswsscommon",
    ],
)

pkg_tar(
    name = "p4rt_binaries",
    testonly = True,  # requred for p4rt_route_measurement_test
    srcs = [
        ":p4rt",  # TODO explore p4rt.stripped
        "//p4rt_app/scripts:p4rt_program_table",
        "//p4rt_app/scripts:p4rt_read",
        "//p4rt_app/scripts:p4rt_route_measurement_test",
        "//p4rt_app/scripts:p4rt_set_forwarding_pipeline",
        "//p4rt_app/scripts:p4rt_write",
    ],
    extension = "tar.bz2",
    mode = "0755",
    package_dir = "/usr/local/bin",
    visibility = ["//visibility:private"],
)

pkg_tar(
    name = "p4rt_debug",
    srcs = [
        ":p4rt.dwp",
    ],
    extension = "tar.bz2",
    mode = "0755",
    package_dir = "/usr/local/bin",
    visibility = ["//visibility:private"],
)

# Here's how to generate the list of Debian packages that contain the shared
# libraries that the p4rt binary depends on. It's probably better to manually
# manage the "depends" list in p4rt_deb, but this can help in debugging when
# a dependency (or transitive dependency) changes.
#
#   ldd bazel-out/k8-fastbuild/bin/p4rt_app/p4rt | \
#     sed "s/^[[:space:]]*\([^ ]*\.so[^ ]*\).*$/\1/" | \
#     xargs -n1 -I{} bash -c "dpkg -S {} 2>/dev/null || true" | \
#     cut -d: -f1 | \
#     sort -u
#
pkg_deb(
    name = "p4rt_deb",
    testonly = True,  # requred for p4rt_route_measurement_test
    architecture = "amd64",
    data = ":p4rt_binaries",
    depends = [
        "libswsscommon",
        "libgmpxx4ldbl",
    ],
    description = "P4RT service",
    maintainer = p4rt_maintainer,
    package = "sonic-p4rt",
    version = p4rt_version,
)

pkg_deb(
    name = "p4rt_dbg_deb",
    architecture = "amd64",
    data = ":p4rt_debug",
    description = "P4RT service debug symbols",
    maintainer = p4rt_maintainer,
    package = "sonic-p4rt-dbgsym",
    version = p4rt_version,
)
